package com.dingding.gmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dingding.gmall.common.utils.PageUtils;
import com.dingding.gmall.common.utils.Query;
import com.dingding.gmall.product.dao.AttrGroupDao;
import com.dingding.gmall.product.entity.AttrEntity;
import com.dingding.gmall.product.entity.AttrGroupEntity;
import com.dingding.gmall.product.service.AttrGroupService;
import com.dingding.gmall.product.service.AttrService;
import com.dingding.gmall.product.vo.AttrGroupWithAttrsVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Long catelogId) {
        //key为检索条件
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(key)) {
            wrapper.and(obj -> obj.eq("attr_group_id",key).or().like("attr_group_name",key));
        }
        if (catelogId.equals(0L)) {
            //catelogId == 0,说明没有3级分类，查询所有的
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        } else {
            wrapper.eq("catelog_id",catelogId);
            IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params), wrapper);
            return new PageUtils(page);
        }
    }

    /**
     * 根据分类id查出所有分组以及这些组的里面的属性
     * @param catelogId
     * @return
     */
    @Override
    public List<AttrGroupWithAttrsVO> getAttrGroupWithAttrsByCatelogId(Long catelogId) {
        //1、查出分组信息
        List<AttrGroupWithAttrsVO> collect = new ArrayList<>();
        List<AttrGroupEntity> attrGroupEntityList = this.list(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", catelogId));
        if (CollectionUtils.isNotEmpty(attrGroupEntityList)) {
            collect = attrGroupEntityList.stream().map(item -> {
                AttrGroupWithAttrsVO attrGroupWithAttrsVO = new AttrGroupWithAttrsVO();
                BeanUtils.copyProperties(item, attrGroupWithAttrsVO);
                List<AttrEntity> attrEntityList = attrService.getRelationAttr(item.getAttrGroupId());
                attrGroupWithAttrsVO.setAttrs(attrEntityList);
                return attrGroupWithAttrsVO;
            }).collect(Collectors.toList());
        }
        return collect;
    }
}